<?php
// +----------------------------------------------------------------------
// | Author: 黄柯柯 <keke.huang@ostengar.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use app\user\model\UserFollowModel;
use app\user\service\UserFollowService;
use cmf\controller\UserBaseController;
use think\Db;

class UserFollowController extends UserBaseController
{

    /**
     * 关注用户
     * @param $params
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function followUser()
    {
        $follow_user_id = $this->request->param('id', 0, 'intval'); //被关注的人id
        $user_id        = cmf_get_current_user_id();

        //判断是否为自己关注自己
        if ($user_id == $follow_user_id) {
            $this->error('不能关注自己！');
        }
        //判断是否已经关注过对方
        $checkUserFollow = cmf_check_user_action("userFollow$follow_user_id", 1);
        if ($checkUserFollow) {
            //变量赋值
            $params['user_id']        = $user_id;
            $params['follow_user_id'] = $follow_user_id;
            $userFollowService        = new UserFollowService();
            $result                   = $userFollowService->followUser($params);
            if ($result) {
                $this->success("关注成功！");
            } else {
                $this->error("操作失败，请稍后再试！");
            }
        } else {
            $this->error("您已经关注过啦！");
        }

    }
    /**
     * 取消关注用户
     * @param $params
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cancelFollowUser()
    {
        $follow_user_id = $this->request->param('id', 0, 'intval'); //取消关注的人id
        $user_id        = cmf_get_current_user_id(); //当前用户id
        //变量赋值
        $params['user_id']        = $user_id;
        $params['follow_user_id'] = $follow_user_id;
        //判断是否已经关注过对方
        $userFollowModel = new UserFollowModel();
        $checkUserFollow = $userFollowModel->where($params)->find();
        if ($checkUserFollow) {
            $userFollowService = new UserFollowService();
            $result            = $userFollowService->cancelFollowUser($params);
            if ($result) {
                $this->success("取消关注成功！");
            } else {
                $this->error("操作失败，请稍后再试！");
            }
        } else {
            $this->error("您还没有关注过对方哟！");
        }
    }
}
